package fun.diaoer.service;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fun.diaoer.common.Contants;
import fun.diaoer.common.StringUtil;
import fun.diaoer.config.security.DiaoerUsernamePasswordAuthenticationToken;
import fun.diaoer.entity.DISysPermission;
import fun.diaoer.entity.DISysUser;
import fun.diaoer.entity.vo.VDISysRole;
import fun.diaoer.entity.vo.VDISysUser;
import fun.diaoer.exception.AuthenException;
import fun.diaoer.mapper.DISysPermissionMapper;
import fun.diaoer.mapper.DISysUserMapper;
import fun.diaoer.service.security.DiaoerUserDetail;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Condition;

@Slf4j
@Service
@Transactional
public class AuthServiceImpl extends AbstractService implements AuthService {
	
	@Autowired
	private DISysUserMapper dISysUserMapper;
	@Autowired
	private DISysPermissionMapper dISysPermissionMapper;
	@Autowired
	private AuthenticationManager authenticationManager;
	
	@Override
	public String getUserId() {
		return super.getUserId();
	}
	
	@Override
	public List<String> getRoleds() {
		return super.getRoleNmes();
	}

	public List<DISysUser> searchUsers(){
		return dISysUserMapper.selectAll();
	}
	
	public List<DISysPermission> getPerms(){
		return super.getPerms();
	}
	
	/**
	 * 查询当前用户下所有权限节点
	 * @return
	 */
	public List<DISysPermission> searchPerms(){
		List<VDISysRole> roles=super.getRoles();
		return dISysPermissionMapper.searchPermsByRoles(roles);
	}
	
	/**
	 * 是否登陆
	 * @return
	 */
	public boolean isLogin() {
		String userId=super.getUserId();
		return StringUtil.isNotBlank(userId)&&!Contants.SYS_USER_ID.equals(userId); 
	}
	
	/**
	 * 注册
	 * @param user
	 * @return
	 */
	public boolean register(VDISysUser user) {
		boolean isExist=isExistUserName(user.getUserName());
		if(isExist) {
			throw new AuthenException("该用户名已存在，请修改用户名！");
		}
		if(!StringUtil.equalsTrim(user.getPassword(), user.getRePassword())) {
			throw new AuthenException("两次密码不一致！");
		}
		int count=dISysUserMapper.insert(user);
		return count>0;
	}
	
	/**
	 * 是否存在用户名
	 */
	public boolean isExistUserName(String userName) {
		Condition con=new Condition(DISysUser.class);
		con.createCriteria().andEqualTo("userName", userName);
		int isHaving=dISysUserMapper.selectCountByCondition(con);
		return isHaving>0;
	}
	
	public String login(DiaoerUserDetail userDetail,HttpServletRequest request) {
		DiaoerUsernamePasswordAuthenticationToken authToken=new  DiaoerUsernamePasswordAuthenticationToken(userDetail.getUsername()+"||~~ADMIN", userDetail.getPassword());
		authToken.setDetails(new WebAuthenticationDetails(request));
		Authentication authenticatedUser = authenticationManager.authenticate(authToken);
		SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
		request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, SecurityContextHolder.getContext());
		return "登陆成功";
	}
	
}
